package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.HhUser;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.service.IHhUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.ParamsDictUtil;
import com.ruoyi.utils.SelectHelpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenjc
 * @since 2020/11/20 9:37
 */
@Controller
public class HhUserController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private IHhUserService hhUserService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPasswordService passwordService;

    @GetMapping("/hh/user/user")
    public String user(ModelMap mmap) {
        HhUser hhUser = new HhUser();
        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
        return prefix + "/user_manage";
    }

    @GetMapping("/hh/user/worker")
    public String worker() {
        return prefix + "/worker_manage";
    }

    @PostMapping("/hh/user/userList")
    @ResponseBody
    public TableDataInfo userList(HhUser hhUser) {
        startPage();
        List<HhUser> list = hhUserService.selectHhUserList(hhUser);
        return getDataTable(list);
    }

    @PostMapping("/hh/user/selectList")
    @ResponseBody
    public AjaxResult selectList(HhUser hhUser) {
        List<HhUser> list = hhUserService.selectHhUserList(hhUser);
        return AjaxResult.success(SelectHelpUtil.getNameUser(list));
    }

    @GetMapping("/hh/user/workerAdd")
    public String workerAdd() {
        return prefix + "/worker_add";
    }

    @Log(title = "新增师傅", businessType = BusinessType.INSERT)
    @PostMapping("/hh/user/workerAdd")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult workerAdd(HhUser hhUser) {
        String phoneNumber = hhUser.getPhonenumber();
        if (StringUtils.isNotEmpty(phoneNumber)
                && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(hhUser))) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
        hhUser.setLoginName(phoneNumber);
        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_WORKER});
        hhUser.setPassword(passwordService.encryptPassword(phoneNumber, ParamsDictUtil.DEFAULT_PASSWORD, null));
        hhUser.setCreateBy(ShiroUtils.getLoginName());
        hhUser.setRemark("水电师傅");
        userService.insertUser(hhUser);
        hhUser.setWorkerId(hhUser.getUserId());
        return toAjax(hhUserService.creatHhUser(hhUser));
    }

    @GetMapping("/hh/user/userAdd")
    public String userAdd(ModelMap mmap) {
        HhUser hhUser = new HhUser();
        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
        return prefix + "/user_add";
    }

    @Log(title = "新增客户", businessType = BusinessType.INSERT)
    @PostMapping("/hh/user/userAdd")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult userAdd(HhUser hhUser) {
        String phoneNumber = hhUser.getPhonenumber();
        if (StringUtils.isNotEmpty(phoneNumber)
                && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(hhUser))) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
        if (hhUserService.selectHhUserByName(hhUser.getUserName()) > 0) {
            return error("新增用户【" + hhUser.getUserName() + "】已存在");
        }
        hhUser.setLoginName(StringUtils.isBlank(phoneNumber) ? String.valueOf(System.currentTimeMillis()) : phoneNumber);
        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_USER});
        hhUser.setPassword(passwordService.encryptPassword(hhUser.getLoginName(), ParamsDictUtil.DEFAULT_PASSWORD, null));
        hhUser.setCreateBy(ShiroUtils.getLoginName());
        userService.insertUser(hhUser);
        //增加系统默认信息
        if (!ParamsDictUtil.DEFAULT_WORKER.equals(hhUser.getWorkerId())) {
            //增加选中的师傅信息
            hhUserService.insertHhUserWorker(hhUser);
            hhUser.setWorkerId(ParamsDictUtil.DEFAULT_WORKER);
        }
        return toAjax(hhUserService.creatHhUser(hhUser));
    }

    @GetMapping("/hh/user/workerEdit/{userId}")
    public String workerEdit(@PathVariable("userId") Long userId, ModelMap mmap) {
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/worker_edit";
    }

    @Log(title = "修改师傅信息", businessType = BusinessType.UPDATE)
    @PostMapping("/hh/user/workerEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult workerEdit(HhUser hhUser) {
        String phoneNumber = hhUser.getPhonenumber();
        if (StringUtils.isNotEmpty(phoneNumber)
                && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(hhUser))) {
            return error("师傅【" + hhUser.getUserName() + "】手机号码已存在");
        }
        hhUser.setLoginName(phoneNumber);
        hhUser.setRoleIds(new Long[]{ParamsDictUtil.ROLE_WORKER});
        hhUser.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(userService.updateUser(hhUser));
    }

    @GetMapping("/hh/user/userEdit/{userId}")
    public String userEdit(@PathVariable("userId") Long userId, ModelMap mmap) {
        HhUser hhUser = new HhUser();
        hhUser.setStatus(ParamsDictUtil.IS_NOT_DEL);
        hhUser.setRoleId(ParamsDictUtil.ROLE_WORKER);
        mmap.put("workerList", hhUserService.selectHhUserList(hhUser));
        mmap.put("user", userService.selectUserById(userId));
        return prefix + "/user_edit";
    }

    @Log(title = "修改客户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/hh/user/userEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult userEdit(HhUser hhUser) {
        String phoneNumber = hhUser.getPhonenumber();
        if (StringUtils.isNotEmpty(phoneNumber)
                && UserConstants.USER_PHONE_NOT_UNIQUE.equals(userService.checkPhoneUnique(hhUser))) {
            return error("新增用户【" + hhUser.getUserName() + "】手机号码已存在");
        }
        hhUser.setLoginName(phoneNumber);
        hhUser.setRoleIds(new Long[]{hhUser.getRoleId()});
        hhUser.setUpdateBy(ShiroUtils.getLoginName());
        hhUserService.updateHhUserWorker(hhUser);
        return toAjax(userService.updateUser(hhUser));
    }

    @PostMapping("/hh/user/userInfo")
    @ResponseBody
    public AjaxResult userInfo(HhUser queryParam) {
        HhUser hhUser = hhUserService.selectHhUserById(queryParam.getUserId());
        if (StringUtils.isBlank(hhUser.getPhonenumber())) {
            hhUser = hhUserService.selectHhUserById(queryParam.getWorkerId());
        }
        return AjaxResult.success(null, hhUser);
    }

}
